package org.itboys.baseobject.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.baseobject.entity.DataDictionary;
import org.itboys.baseobject.service.DataDictionaryService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


/**
 * 字典管理
 * 
 * @author Administrator
 * 
 */

@RestController
@RequestMapping("/DataDictionaryController")
public class DataDictionaryController extends BaseController {

	@Autowired
	private DataDictionaryService dictService;
	@Autowired
	private ResourceHolder resourceHolder;

	@RequestMapping("/toVM")
	public ModelAndView listvm(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/sys/datadictionary");
	}
	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/list")
	public void list(@RequestParam(value="type" ,required=false) Integer type, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		if (type != null) {
			sqlMap.put("type", type);
		}
		Page<DataDictionary> page = dictService.pageQuery(sqlMap, request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	/**
	 * 新增修改时保存
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(@ModelAttribute DataDictionary dict, HttpServletRequest request, HttpServletResponse response) {
		try {
			if(dict.getId() != 0) {
				dictService.update(dict);
			} else {
				dictService.save(dict);
			}
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response, "fail");
		}
	}

	/**
	 * 根据Id查询
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/findById")
	public void findById(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
		DataDictionary dict = dictService.getById(id);
		AjaxUtils.renderJson(response, dict);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
		try {
			dictService.delete(id);
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			AjaxUtils.renderText(response, "fail");
		}
	}

	/**
	 * 根据类型获取信息
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getByType")
	public void getAll(@RequestParam Integer type, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		List<DataDictionary> list = dictService.list(map);
		AjaxUtils.renderJson(response, list);
	}
}
